/**
 * Created by DukeCrushIt on 2015/7/27.
 */
class CreateRoleScene extends eui.Component {
    public selectedGender:number;
    public selectedType:number;
    private maleNames:string[];
    private femaleNames:string[];
    public constructor(){
        super();
        this.skinName = "pet.CreateRoleSkin";// "resource/skins/CreateRoleSkin";
        this.addEventListener(egret.Event.ADDED_TO_STAGE,this.addedToStage, this );

    }

    private onGenderSelect(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        if( evt.target == this.img_male  ){
            this.selectGender(Model.MALE);
        }else if(  evt.target == this.img_female ){
            this.selectGender(Model.FEMALE);
        }
    }

    private onSelectType(evt:egret.TouchEvent){
        if( evt.target == this.img_puppy || evt.target == this.select_dog){
            this.selectType( Model.GOOD_TYPE_DOG);
            SoundMgr.getInstance().playEffect(SoundMgr.BARK_1);
        }else{
            this.selectType( Model.GOOD_TYPE_CAT);
            SoundMgr.getInstance().playEffect(SoundMgr.MIOW_1);
        }
        this.selectType(evt.target == this.img_puppy ? Model.GOOD_TYPE_DOG : Model.GOOD_TYPE_CAT);
        if(evt.target == this.img_puppy){
           SoundMgr.getInstance().playEffect(SoundMgr.BARK_1);
        }else{
           SoundMgr.getInstance().playEffect(SoundMgr.MIOW_1);
        }
    }

    public selectGender(gender:number){
        this.selectedGender = gender;
        this.img_male.source = RES.getRes(gender == Model.MALE ? "select_1" : "select_0");
        this.img_female.source = RES.getRes(gender == Model.FEMALE ? "select_1":"select_0");
        this.onRandomName(null);
    }

    public selectType(type:number){
        if( this.selectedType == type) return;
        this.selectedType = type;
        if( this.selectedType == Model.GOOD_TYPE_DOG){
            var tw:egret.Tween = egret.Tween.get(this.img_puppy);
            tw.to({x:250,y:450,scaleX:1,scaleY:1},300);

            tw = egret.Tween.get(this.img_kitty);
            tw.to({x:410,y:340,scaleX:0.4,scaleY:0.4},300);
            this.select_dog.source = RES.getRes("btn_dog_1");
            this.select_cat.source = RES.getRes("btn_cat_0");
        }else{
            tw = egret.Tween.get(this.img_puppy);
            tw.to({x:60,y:340,scaleX:0.4,scaleY:0.4},300);

            tw = egret.Tween.get(this.img_kitty);
            tw.to({x:220,y:450,scaleX:1.0,scaleY:1.0},300);
            this.select_dog.source = RES.getRes("btn_dog_0");
            this.select_cat.source = RES.getRes("btn_cat_1");
        }
    }

    private onRandomName(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        var arr:string[] = this.selectedGender == 0 ? this.maleNames : this.femaleNames;
        var randomIndex:number = Math.round(arr.length * Math.random());
        this.name_input.text = arr[randomIndex];
    }
    private DOG_ID:number = 1021010;
    private CAT_ID:number = 1011005;
    private onConfirm(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        window["__buy"](this.name_input.text,this.selectedType == Model.GOOD_TYPE_DOG ? this.DOG_ID : this.CAT_ID,this.selectedGender);
    }

    public onBuyBack(){
        DisplayController.instance().removeUiElement(this);
        if( RES.getGroupByName("preload") != undefined){
            PetMgr.getInstance().start();
            DisplayController.instance().addUiElement(MainScene.getInstance());
            DisplayController.instance().addUiElement(MainTab.getInstance());
            DisplayController.instance().addUiElement(DiamondElement.getInstance());
            DiamondElement.getInstance().updateDisplay();
            SceneMgr.getInstance().switchScene(SceneMgr.SCENE_MAIN);
            Model.checkGuide("create");
            RES.destroyRes("create");
            // this.img_puppy.content.dispose();
            // this.img_kitty.content.dispose();
            window["__lattention"]();
        }
        //window["__guide"](1);
    }

    public select_dog:eui.Image;
    public select_cat:eui.Image;
    public img_male:eui.Image;
    public img_female:eui.Image;

    public img_puppy:eui.Image;
    public img_kitty:eui.Image;

    public name_input:eui.TextInput;
    public random_btn:eui.Image;

    public btn_confirm:eui.Button;
    // public partAdded(name:string, instance:any){
    //     super.partAdded(name, instance);
    //    if(name == "name_input"){
    //         this.name_input = instance;
    //     }else if(name == "random_btn"){
    //         this.random_btn = instance;
    //     }else if(name == "btn_confirm"){
    //         this.btn_confirm = instance;
    //     }else if(name == "select_dog"){
    //         this.select_dog = instance;
    //     }else if(name == "select_cat"){
    //         this.select_cat = instance;
    //     }else if(name == "img_male"){
    //         this.img_male = instance;
    //     }else if(name == "img_female"){
    //         this.img_female = instance;
    //     }else if(name == "img_puppy"){
    //         this.img_puppy = instance;
    //         this.img_puppy.source = "resource/assets/petpreview/1021010_c.png";
    //     }else if(name == "img_kitty"){
    //         this.img_kitty = instance;
    //         this.img_kitty.source = "resource/assets/petpreview/1011005_c.png";
    //     }
    // }

    private addedToStage(evt:egret.Event){
        if( this.femaleNames == null){
            var names:string = RES.getRes("names_cvs");
            var tempArr:string[] = names.split(":");
            this.maleNames = tempArr[0].split(",");
            this.femaleNames = tempArr[1].split(",");
        }
        this.selectType(Math.random() > 0.5 ? Model.GOOD_TYPE_CAT : Model.GOOD_TYPE_DOG);
        this.selectGender(Math.random() > 0.5 ? 0 : 1);
        // this.img_puppy.addEventListener(eui.UIEvent.UPDATE_COMPLETE,this.updateAnchor, this);
        // this.img_kitty.addEventListener(eui.UIEvent.UPDATE_COMPLETE,this.updateAnchor, this);
        this.img_puppy.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onSelectType, this);
        this.img_kitty.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onSelectType, this);
        this.random_btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onRandomName, this);
        this.btn_confirm.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onConfirm, this);
        this.img_male.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGenderSelect, this);
        this.img_female.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGenderSelect, this);
        this.select_dog.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onSelectType, this);
        this.select_cat.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onSelectType, this);
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.removedFromStage, this);
        this.removeEventListener(egret.Event.ADDED_TO_STAGE,this.addedToStage, this );
    }

    private removedFromStage(evt:egret.Event){
        this.img_puppy.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onSelectType, this);
        this.img_kitty.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onSelectType, this);
        this.random_btn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onRandomName, this);
        this.btn_confirm.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onConfirm, this);
        this.img_male.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onGenderSelect, this);
        this.img_female.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onGenderSelect, this);
        // this.img_puppy.removeEventListener(eui.UIEvent.UPDATE_COMPLETE,this.updateAnchor, this);
        // this.img_kitty.removeEventListener(eui.UIEvent.UPDATE_COMPLETE,this.updateAnchor, this);
        this.select_dog.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onSelectType, this);
        this.select_cat.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onSelectType, this);
        this.removeEventListener(egret.Event.REMOVED_FROM_STAGE, this.removedFromStage, this);
    }

    // private updateAnchor(evt:eui.UIEvent){
    //     var tar:any = evt.target;
    //     AnchorUtil.setAnchor(tar,0.5,1);
    // }

    private static _instance:CreateRoleScene;
    public static getInstance():CreateRoleScene{
        if( CreateRoleScene._instance == null)
            CreateRoleScene._instance = new CreateRoleScene();
        return CreateRoleScene._instance;
    }
}